package org.jcms.web.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jcms.model.Order;
import org.jcms.model.PageBean;
import org.jcms.service.OrderServiceI;
import org.jcms.uitils.DateUtil;
import org.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 * type 0 查看所有审核通过未付款的订单 type 1 查看指定用户所有审核通过未付款的订单 其它参数名 customerId
 * 
 * @author Administrator
 * 
 */

public class ManagerGetUnPayOrder extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public ManagerGetUnPayOrder() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.getServletContext());
		OrderServiceI orderServiceI = (OrderServiceI) ac
				.getBean("orderServiceImpl");
		String page = request.getParameter("page");
		String rows = request.getParameter("rows");
		String type = request.getParameter("type");
		String customerId = request.getParameter("customerId");

		String retJson = "";
		int intPage = 1;
		int intRows = 10;

		JSONObject jsonObject = new JSONObject();

		try {

			int Itype = Integer.parseInt(type);
			intPage = Integer.parseInt(page);
			intRows = Integer.parseInt(rows);
			if (intPage <= 0) {
				intPage = 1;
			}
			if (intRows > 200) {
				intRows = 10;
			}
			if (Itype != 0 && Itype != 1) {
				Itype = 0;
			}

			List<Order> orders = null;
			PageBean<Order> pbOrder = null;
			switch (Itype) {
			case 0:
				String hql = "from Order where state.stateId = 1 order by orderTime";
				pbOrder = orderServiceI.findByPage(hql, intPage, intRows);
				break;
			case 1:
				if (customerId == null || "".equals(customerId)) {
					return;
				}
				hql = "from Order where state.stateId = 1 and customer.customerId = ? order by orderTime";
				pbOrder = orderServiceI.findByPage(hql, intPage, intRows,
						customerId);
				break;
			default:
				break;
			}

			jsonObject.put("total", pbOrder.getTotalRows());

			orders = pbOrder.getData();

			List<HashMap<String, String>> orderls = new ArrayList<HashMap<String, String>>();
			for (Order order : orders) {
				HashMap<String, String> tempMap = new HashMap<String, String>();
				tempMap.put("orderId", order.getOrderId());
				tempMap.put("customerId", order.getCustomer().getCustomerId());
				tempMap.put("orderSum", order.getOrderSum() + "");
				tempMap.put("orderTime",
						DateUtil.formatDatetime(order.getOrderTime()));
				tempMap.put("stateName", order.getState().getStateName());
				orderls.add(tempMap);
			}

			jsonObject.put("rows", orderls);
			retJson = jsonObject.toString();
		}

		catch (Exception e) {
         e.printStackTrace();
		}

		finally {
			response.getOutputStream().write(retJson.getBytes("utf-8"));
		}

	}

	public void init() throws ServletException {

	}

}
